//
//  HSHWDetailsViewModel.swift
//  HomeSchoolDemo
//
//  Created by rimi on 2016/10/13.
//  Copyright © 2016年 黄华强. All rights reserved.
//

import UIKit
import SVProgressHUD

class HSHWDetailsListViewModel {
    
    //模型数组
    var objectiveTopicArray = [HSHW_htViewModel]()
    var subjectiveTopicArray = [HSHW_htViewModel]()
    
    var detailsArray:[[HSHW_htViewModel]] = []
    //请求数据
    func requestDetails(hwID:Int, finished: @escaping callback) {
        
        detailsArray.append(objectiveTopicArray)
        detailsArray.append(subjectiveTopicArray)
        
        let url = "ZH-schoolname-S-getHomeworkCheck"
        let key = userInfo["key"] as! String
        
        let param = ["key":key,"hw_id":hwID] as [String : Any]
        
        HSNetWorkTools.shared.downloadData(url: url, parameters: param) { [weak self] (data, error) in
            
            guard let data = data as? NSDictionary else {
                SVProgressHUD.showInfo(withStatus: "网络请求错误！")
                finished(error, false)
                return
            }
            
            switch data["result"] as! Int {
            case 0:
                let dictArr = data["data"] as! [[String:Any]]
                for dict in dictArr {
                    
                    let model = HSHW_htModel(dict: dict)
                    if model.ht_type == 4 {
                       
                        if let optionArr = model.ht_option {
                            var optionMArr = [HSSubjectiveOptionModel]()
                            for oDict in optionArr {
                                optionMArr.append(HSSubjectiveOptionModel(dict: oDict as! [String : Any]))
                            }
                            model.ht_option = optionMArr
                        }

                        self?.detailsArray[1].append(HSHW_htViewModel(model: model))
                    } else {
                        
                        if let optionArr = model.ht_option {
                            var optionMArr = [HSObjectiveOptionModel]()
                            
                            for oDict in optionArr {
                                optionMArr.append(HSObjectiveOptionModel(dict: oDict as! [String : Any]))
                            }
                            model.ht_option = optionMArr
                        }
                        self?.detailsArray[0].append(HSHW_htViewModel(model: model))
                    }
                }
                
                finished(nil, true)
                
            case 1:
                HSAlertView.bottomAlertView("数据请求错误")
                finished(nil,false)
            default:
                break
            }
            
        }
        
    }
    
}
